package jp.sourceforge.akjrcp.akj_betas;



import jp.sourceforge.akjrcp.draw2d.PerspectiveHelpButton;
import jp.sourceforge.akjrcp.dropsomething.DropsomethingPerspective;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.Panel;
import org.eclipse.draw2d.ScrollBar;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.IntroPart;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class Akj_betasIntro extends IntroPart{
	IntroPart part=this;
	private Canvas canvas;
	private ScrollPane scrollPane;
	public void createPartControl(Composite parent) {
		
		parent.setLayout(new GridLayout(2,false));//
		
		Button bt=new Button(parent,SWT.NULL);
		 ImageDescriptor icon_line = AbstractUIPlugin.imageDescriptorFromPlugin(
		    		"jp.sourceforge.akjrcp.akj_betas", "icons/warn_installsoftware.png");
		 bt.setImage(icon_line.createImage());
		 
		bt.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e) {
				
					//part.getIntroSite().getPage().setEditorAreaVisible(false);
			//		IWorkbenchPage w=PlatformUI.getWorkbench().showPerspective(Perspective.ID,PlatformUI.getWorkbench().getActiveWorkbenchWindow());
					PlatformUI.getWorkbench().getIntroManager().closeIntro(part);
					
					//w.bringToTop();
					//w.set
					//PlatformUI.getWorkbench().getActiveWorkbenchWindow().(Perspective.ID,getIntroSite());
				
			}

		});
		canvas = new Canvas(parent,SWT.NO_BACKGROUND);
		canvas.addMouseTrackListener(new MouseTrackAdapter()
		{
		
			public void mouseEnter(MouseEvent e)
			{
				canvas.forceFocus();
			}
		});
		canvas.addListener(SWT.MouseWheel, new Listener()
		{
			public void handleEvent(Event event)
			{
				int c = event.count;
				c = (int) Math.ceil(c / 3.0f);
				while (c < 0)
				{
					scrollUp();
					c++;
				}
				
				while (c > 0)
				{
					scrollDown();
					c--;
				}
				//log.info(c);
			}
		});
		//canvas.setBackground(new Color(null,0,128,0));
		
		canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
		LightweightSystem lws = new LightweightSystem(canvas);
		scrollPane = new ScrollPane();
		scrollPane.setScrollBarVisibility(ScrollPane.ALWAYS);
		scrollPane.getVerticalScrollBar().setStepIncrement(20);
		Panel panel=new Panel();
		
		
		//panel.setBackgroundColor(new Color(null,128,0,0));
		FlowLayout layout=new FlowLayout(false);
		layout.setStretchMinorAxis(true);//streach horizontal
		panel.setLayoutManager(layout);
		
		
		for (int i = 0; i <30; i++) {
			PerspectiveHelpButton rr=new PerspectiveHelpButton();
			
			//rr.setBounds(new Rectangle(0,0,300,100));
			rr.setForegroundColor(ColorConstants.white);
			rr.setBackgroundColor(ColorConstants.lightGray);
			rr.setPerspectiveID(DropsomethingPerspective.ID);
			panel.add(rr);
		}
		
		
		scrollPane.setContents(panel);
		
		lws.setContents(scrollPane);
		
	}
	
	public void scrollDown(){
		ScrollBar bar=scrollPane.getVerticalScrollBar();
		bar.setValue(bar.getValue() - bar.getStepIncrement());
	}

	public void scrollUp(){
		ScrollBar bar=scrollPane.getVerticalScrollBar();
		bar.setValue(bar.getValue() + bar.getStepIncrement());
	}
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}

	public void standbyStateChanged(boolean standby) {
		// TODO Auto-generated method stub
		
	}



}
